package com.mattemanouil.DriveAway;


import com.tractivity.notifications.TTNotificationManager; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 

 
public class DriveAwayActivity extends Activity implements OnClickListener, OnSharedPreferenceChangeListener { 
     
     private SharedPreferences prefs; 
     private boolean previousActiveAwayMessage = false; 
     private TextView tv; 
      
     
     /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        // Set on click listener for away message button 
        View awayMessageButton = findViewById(R.id.awayMessageButton); 
        awayMessageButton.setOnClickListener(this); 
                         
        // Set text of text view to current away message       
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        tv = (TextView)findViewById(R.id.testPreferences); 
        tv.setText(prefs.getString("awayMessageListPreference", "Please select an away message.")); 
                       
        prefs.registerOnSharedPreferenceChangeListener(this);     
                         
    } 
     
    @Override 
    protected void onStart() { 
         super.onStart(); 
         if(prefs.getString("awayMessageListPreference", "Please select an away message.").equals("No away message")) { 
               // Nothing 
          } 
         else { 
              TTNotificationManager.notification().setNotification(this, "Away Message", "Away Message", prefs.getString("awayMessageListPreference", "No away message"), R.drawable.paper_icon, DriveAwayActivity.class); 
             TTNotificationManager.notification().startNotification(); 
         } 
    } 
     
    @Override 
    protected void onStop() { 
         super.onStop(); 
    } 
     
    @Override 
    protected void onRestart() { 
         super.onRestart(); 
          
    } 
     
    @Override 
    protected void onResume() { 
         super.onResume(); 
         // Set text of text view to current away message       
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        tv.setText(prefs.getString("awayMessageListPreference", "Please select an away message.")); 
    } 
     
    @Override 
    protected void onPause() { 
         super.onPause(); 
    } 
     
    @Override 
    protected void onDestroy() { 
         super.onDestroy(); 
         TTNotificationManager.notification().cancelNotification(); 
    } 
     
    /* onClick method */ 
    public void onClick(View v) { 
           
          switch(v.getId()) { 
               /* When the away message button is pressed */ 
               case R.id.awayMessageButton: 
                    Intent i = new Intent(this, Preferences.class); 
                    startActivity(i); 
                    break; 
          } 
                
     } 
 
    /* Listen for preference changes and start and stop the service based on 
     * the current preference. 
     */ 
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {           
          // If away message is set to no away message, cancel notification. 
          if(prefs.getString("awayMessageListPreference", "Please select an away message.").equals("No away message")) { 
               TTNotificationManager.notification().cancelNotification(); 
          } 
          // If away message is not set to no away message. 
          else { 
               // If the away message was originally no away message 
               if(previousActiveAwayMessage == false) { 
                    TTNotificationManager.notification().setNotification(this, "Away Message", "Away Message", prefs.getString("awayMessageListPreference", "0"), R.drawable.paper_icon, DriveAwayActivity.class); 
                  TTNotificationManager.notification().startNotification(); 
                    previousActiveAwayMessage = true; 
               }      
               else { 
                    TTNotificationManager.notification().updateNotification(this, "Away Message", prefs.getString("awayMessageListPreference", "0"), R.drawable.paper_icon, DriveAwayActivity.class); 
               } 
          } 
          
          
          
     } 
      
}